package cn.tiger.starter.poi;

import cn.tiger.entity.User;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

/**
 * @Author ： ZAG WELL
 * @Date : 2021/5/4
 */
public class POIExport {
    public static ArrayList<User> users = new ArrayList<>();

    static {
        User user = new User();
        user.setAccount("aaa");
        user.setUser("11");
        user.setPassword("123123");
        users.add(user);

        User user1 = new User();
        user1.setAccount("bbb");
        user1.setUser("222");
        user1.setPassword("123123");
        users.add(user1);

        User user2 = new User();
        user2.setAccount("ccc");
        user2.setUser("333");
        user2.setPassword("123123");
        users.add(user2);
    }
    public static void main(String[] args){
        System.out.println(users);
        // 创建工作簿，内存中
        Workbook workbook = new XSSFWorkbook();
        // 创建工作表
        Sheet sheet = workbook.createSheet("测试写excel");
        Row fristRow = sheet.createRow(0);
        fristRow.createCell(0).setCellValue("account");
        fristRow.createCell(1).setCellValue("user");
        fristRow.createCell(2).setCellValue("password");
        //创建HSSFRow对象
        for (int i = 1; i < users.size() + 1; i++) {
            Row row = sheet.createRow(i);
            //创建HSSFCell对象
            row.createCell(0).setCellValue(users.get(i-1).getUser());
            row.createCell(1).setCellValue(users.get(i-1).getAccount());
            row.createCell(2).setCellValue(users.get(i-1).getPassword());
            System.out.println("填充第"+i+"行数据");
        }
        //输出Excel文件
        FileOutputStream output= null;
        try {
            output = new FileOutputStream("C:\\Users\\ZAG WELL\\Desktop\\测试导出导出\\testExport.xlsx");
            workbook.write(output);
            output.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
